import {
  Controller,
  Post,
  Request,
  Body,
  UseGuards,
  Session,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { LocalAuthGuardUser } from './guards/local-auth.guard';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { LoginUserDto } from './dto/login-user.dto';

@ApiTags('user auth')
@Controller('api/auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @UseGuards(LocalAuthGuardUser)
  @Post('login')
  @ApiOperation({ summary: '用户登录' })
  // async login(@Request() req) {
  //   console.log('user auth ctrl', req.user);
  //   return this.authService.login(req.user);
  // }
  async login(
    @Body() loginUserDto: LoginUserDto,
    @Request() req,
    @Session() session: Record<string, any>,
  ) {
    console.log('loginUserDto', loginUserDto, req.session);

    const { captcha: reqCaptcha } = loginUserDto;
    const { captcha } = session;

    console.log('reqCaptcha', reqCaptcha, captcha);

    // if (reqCaptcha.toLocaleLowerCase() !== captcha.toLocaleLowerCase()) {
    //   throw new HttpException(
    //     {
    //       status: 202,
    //       error: '验证码不正确',
    //     },
    //     202,
    //   );
    // }
    return this.authService.login(req.user);
  }

  @Post('logout')
  @ApiOperation({ summary: '用户登出' })
  async logout() {
    return this.authService.logout();
  }
}
